using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


public class EnemyAttackDataHolder : MonoBehaviour
{
    [SerializeField]
    public EnemyAttackData_SO so;//因为怪物行为是不变的，就绑定了，在事件中产生随机效果，比如回合数为2的事件数据有两个，则随机其中一个执行
    public EnemyAttackPreview preview;


    //public event Action<AttackEveryRound, CharacterDataHolder, CharacterDataHolder> enemyAttackAction;
    private AttackEveryRound nextAttack;


    public void DoAttack(int attackerNum,int currentRound)
    {
        preview.HideCurrentPreview();
        CharacterActionData actionData = new();
        actionData.ReadFromAttackEveryRound(nextAttack);
        if(actionData.isAimSelf)
        {
            actionData.chooseCharacterOrder = attackerNum;
        }
        actionData.actionNum += currentRound * actionData.roundInfluenceNum;
        ChooseEnemyAction(actionData);
        //enemyAttackAction?.Invoke(nextAttack, attacker, defender);
    }

    public void SetNextTurnAttack(int round)
    {
        Debug.Log("Round:"+round);
        List<AttackEveryRound> aerList = new();
        int soLength = GetAttackLengthInSO(so);
        foreach (var item in so.attackList)
        {
            if (round % soLength == 0)
            {
                if (item.round == soLength)
                {
                    aerList.Add(item);
                }
            }
            else
            {
                if (item.round == (round % soLength))//求余数使怪物得以循环攻击
                {
                    aerList.Add(item);
                }
            }
        }
        int randomNum = RandomEngine.Instance.GetRandCombatRewards(0, aerList.Count);
        nextAttack = aerList[randomNum];
        preview.ShowAttackPreview(nextAttack.actionNum+nextAttack.roundInfluenceNum*round, nextAttack.previewImageType);
    }

    private int GetAttackLengthInSO(EnemyAttackData_SO so)
    {
        int round = 1;
        //Debug.LogWarning(so);这里是第二次重新载入场景后报错的消失变量
        foreach (var item in so.attackList)
        {
            if (item.round == round)
            {
                round++;
            }
        }
        return round - 1;
    }

    private void ChooseEnemyAction(CharacterActionData data)
    {
        switch (data.actionType)
        {
            case 0://格挡
                switch (data.chooseCharacterType)
                {
                    case 0://对友军
                        StartCoroutine(CharacterActions.TakeDefenceForFriend(data));
                        break;
                    case 1://对敌人
                        StartCoroutine(CharacterActions.TakeDefenceForEnemy(data));
                        break;
                }
                break;
            case 1://伤害
                switch (data.chooseCharacterType)
                {
                    case 0://对友军
                        StartCoroutine(CharacterActions.AttackFriend(data));
                        break;
                    case 1://对敌人
                        StartCoroutine(CharacterActions.AttackEnemy(data));
                        break;
                }
                break;
            case 2://胆魄
                switch (data.chooseCharacterType)
                {
                    case 0://对友军
                        StartCoroutine(CharacterActions.TakeCourgeForFriend(data));
                        break;
                    case 1://对敌人
                        StartCoroutine(CharacterActions.TakeCourgeForEnemy(data));
                        break;
                }
                break;
        }
    }
}
